package com.store.mapper;

import com.store.entity.Product;
import org.apache.ibatis.annotations.*;
import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface ProductMapper {
    @Select("SELECT * FROM product ORDER BY product_id DESC LIMIT #{offset}, #{pageSize}")
    List<Product> selectList(@Param("offset") int offset, @Param("pageSize") int pageSize);

    @Select("SELECT * FROM product WHERE product_id = #{productId}")
    Product selectById(Long productId);

    @Select("SELECT * FROM product WHERE category_id = #{categoryId}")
    List<Product> selectByCategoryId(Long categoryId);

    @Insert("INSERT INTO product (product_name, category_id, product_price, stock, image_url, description) " +
            "VALUES (#{productName}, #{categoryId}, #{productPrice}, #{stock}, #{imageUrl}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "productId")
    int insert(Product product);

    @Update("UPDATE product SET product_name = #{productName}, category_id = #{categoryId}, " +
            "product_price = #{productPrice}, stock = #{stock}, image_url = #{imageUrl}, " +
            "description = #{description} WHERE product_id = #{productId}")
    int update(Product product);

    @Delete("DELETE FROM product WHERE product_id = #{productId}")
    int deleteById(Long productId);

    @Select("SELECT COUNT(*) FROM product")
    long count();

    @Select("SELECT COUNT(*) FROM product WHERE category_id = #{categoryId}")
    long countByCategoryId(Long categoryId);

    @Select("SELECT * FROM product WHERE product_name LIKE CONCAT('%', #{keyword}, '%') LIMIT #{offset}, #{pageSize}")
    List<Product> selectListByKeyword(@Param("keyword") String keyword, @Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

    @Select("SELECT * FROM product WHERE category_id = #{categoryId} LIMIT #{offset}, #{pageSize}")
    List<Product> selectListByCategory(@Param("categoryId") Long categoryId, @Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

    @Select("SELECT * FROM product ORDER BY product_id DESC LIMIT 1")
    Product selectLatestProduct();

    @Select("SELECT * FROM product ORDER BY product_id DESC LIMIT 1")
    Product selectTopSalesProduct();

    @Select("SELECT 0")
    BigDecimal selectProductSales(@Param("productId") Long productId);

    @Select("SELECT DISTINCT category_id FROM product")
    List<String> selectAllCategories();
} 